﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.BLL;
using System.Data;

namespace EnvironmentManagerSystem.ExpertManager
{
    public partial class ChangeExpert : System.Web.UI.Page
    {
        public string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                BingData();

                //Jscript.Alert(id.ToString(), this);
                ExpertData model = Expert.GetModelByID(id)[0];
                txtBirthday.Text = model.Birthday.Trim();
                txtCertNo.Text = model.CardNo.Trim();
                txtCompany.Text = model.Work.Trim();
                txtEMail.Text = model.Email.Trim();
                txtExpertName.Text = model.Name.Trim();
                txtExpertType.SelectedValue = model.ExpertType.Trim();
                txtFax.Text = model.Fax.Trim();
                txtMobile.Text = model.Mobile.Trim();
                txtNowCompany.Text = model.NowWork.Trim();
                txtPhone.Text = model.Phone.Trim();
                txtRecommendation.Text = model.Recommendation.Trim();
                txtSex.Text = model.Sex.Trim();
                txtTitle.Text = model.Title.Trim();
                TextBox1.Text = model.ExpertMemo.Trim();
                //对listbox的绑定
                string[] choose = model.BusinessType.Split(',');
                string[] choose1 = model.EIAType.Split(',');

                foreach (string str in choose)
                {
                    if(str!="")
                    {
                        lbChoose.Items.Add(str);
                    }
                }
                foreach (string str in choose1)
                {
                    if (str != "")
                    {
                        lbChoose1.Items.Add(str);
                    }
                }

                ExpertIndustryKindOneDataList lst1 = ExpertIndustryKindOne.GetList();
                ExpertIndustryKindTwoDataList lst2 = ExpertIndustryKindTwo.GetList();

                foreach (ExpertIndustryKindOneData model1 in lst1)
                {
                    int j = 0;
                    for (int i = 0; i < lbChoose.Items.Count;i++)
                    {
                        if ((model1.ExpertIndustryKindOneCode+model1.ExpertIndustryKindOneName) == lbChoose.Items[i].Value)
                        {
                            j = 1;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (j == 1)
                    {

                    }
                    else
                    {
                        lbUnchoose.Items.Add(model1.ExpertIndustryKindOneCode+model1.ExpertIndustryKindOneName);
                    }
                }

                foreach (ExpertIndustryKindTwoData model1 in lst2)
                {
                    int j = 0;
                    for (int i = 0; i < lbChoose1.Items.Count; i++)
                    {
                        if ((model1.ExpertIndustryKindTwoCode + model1.ExpertIndustryKindTwoName) == lbChoose1.Items[i].Value)
                        {
                            j = 1;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (j == 1)
                    {

                    }
                    else
                    {
                        lbUnChoose1.Items.Add(model1.ExpertIndustryKindTwoCode + model1.ExpertIndustryKindTwoName);
                    }
                }
                
            }
        }

        public void BingData()
        {
            // 绑定专家类型下拉菜单
            ExpertKindDataList ekl = ExpertKind.GetList();
            if (ekl != null)
            {
                foreach (ExpertKindData model in ekl)
                {
                    txtExpertType.Items.Add(model.ExpertKindCode.Trim() + model.ExpertKindName.Trim());
                }
            }
            //绑定行业领域
            //ExpertIndustryKindOneDataList eikodl = ExpertIndustryKindOne.GetList();
            //if (eikodl != null)
            //{
            //    foreach (ExpertIndustryKindOneData model in eikodl)
            //    {
            //        lbUnchoose.Items.Add(model.ExpertIndustryKindOneCode.Trim() + model.ExpertIndustryKindOneName.Trim());
            //    }
            //}
            //绑定评估类别





        }

        protected void btnChoose_Click(object sender, EventArgs e)
        {
            if (lbUnchoose.SelectedValue != "")
            {
                lbChoose.Items.Add(lbUnchoose.SelectedValue);
                lbUnchoose.Items.RemoveAt(lbUnchoose.SelectedIndex);
            }
        }

        protected void btnUnChoose_Click(object sender, EventArgs e)
        {
            if (lbChoose.SelectedValue != "")
            {
                lbUnchoose.Items.Add(lbChoose.SelectedValue);
                lbChoose.Items.RemoveAt(lbChoose.SelectedIndex);
            }
        }

        protected void btnChoose1_Click(object sender, EventArgs e)
        {
            if (lbUnChoose1.SelectedValue != "")
            {
                lbChoose1.Items.Add(lbUnChoose1.SelectedValue);
                lbUnChoose1.Items.RemoveAt(lbUnChoose1.SelectedIndex);
            }
        }

        protected void btnUnChoose1_Click(object sender, EventArgs e)
        {
            if (lbChoose1.SelectedValue != "")
            {
                lbUnChoose1.Items.Add(lbChoose1.SelectedValue);
                lbChoose1.Items.RemoveAt(lbChoose1.SelectedIndex);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            ExpertData model = new ExpertData();
            model.Name = txtExpertName.Text.Trim();
            model.Birthday = txtBirthday.Text.Trim();
            //组建行业类别
            string BusinessType = "";
            for (int i = 0; i < lbChoose.Items.Count; i++)
            {
                BusinessType = BusinessType + lbChoose.Items[i];
            }
            model.BusinessType = BusinessType.Trim();
            model.CardNo = txtCertNo.Text.Trim();
            string EIAType = "";
            for (int i = 0; i < lbChoose1.Items.Count; i++)
            {
                EIAType = EIAType + lbChoose1.Items[i];
            }
            model.EIAType = EIAType.Trim();
            model.Email = txtEMail.Text.Trim();
            model.ExpertMemo = TextBox1.Text.Trim();
            model.ExpertType = txtExpertType.SelectedValue;
            model.Fax = txtFax.Text.Trim();
            model.Mobile = txtMobile.Text.Trim();
            model.NowWork = txtNowCompany.Text.Trim();
            model.Phone = txtPhone.Text.Trim();
            model.Recommendation = txtRecommendation.Text.Trim();
            model.Sex = txtSex.Text.Trim();
            model.Title = txtTitle.Text.Trim();
            model.Work = txtCompany.Text.Trim();
            model.ID = Convert.ToInt32(id);
            if (Expert.Update(model) > 0)
            {
                Jscript.Alert("修改成功!", this);
                Response.Write("<script>window.parent.location.href = 'ExpertManagerPage.aspx';</script>");
            }
            else
            {
                Jscript.Alert("修改失败!", this);
            }
        }
    }
}
